<?php
namespace app\controller;

use app\service\IndexService;
use support\View;
use support\think\Cache;
class Base
{
    protected $system;
    public function __construct()
    {
        $base= new IndexService();
        //系统配置
        if(config('app.debug',false)){
            $system=$base->getsystem();
        }else{
            if(!$system=Cache::get('system')){
                $system=$base->getsystem();
                Cache::set('system',$system,3600);
            }
        }
        $this->system=$system;
        View::assign('system', $system);
        //分配广告
        if(config('app.debug',false)){
            $ad=$base->ad();
        }else{
            if(!$ad=Cache::get('ad')){
                $ad=$base->ad();
                Cache::set('ad',$ad,3600);
            }
        }
        View::assign('ad', $ad);
        //使用缓存
        $action=request()->action;
        $id=request()->input('id',0);
        if($action=='index'){
            if(config('app.debug',false)){
                $dataindex=[
                    'catename'=>'首页',
                    'en_name'=>'index',
                    'fid'=>0,
                    'cateid'=>0
                ];
            }else{
                if(!$dataindex=Cache::get('dataindex'.$id)){
                    $dataindex=[
                        'catename'=>'首页',
                        'en_name'=>'index',
                        'fid'=>0,
                        'cateid'=>0
                    ];
                    Cache::set('dataindex',$dataindex,3600);
                }
            }
            View::assign('data', $dataindex);
        }

        if($action=='cate'){
            if(config('app.debug',false)){
                $datacate=(new \app\model\CateModel())->find($id);
            }else{
                if(!$datacate=Cache::get('datacate'.$id)){
                    $datacate=(new \app\model\CateModel())->find($id);
                    Cache::set('datacate'.$id,$datacate,3600);
                }
            }
            if(!$datacate){
                header("Location:/404.html");
                die;
            }
            $datacate['cateid']=$id;
            View::assign('data', $datacate);
            //这里做跳转判断
            if($datacate['tiao_type']==1){
                redirect((string)url('index/cate', ['id' => $datacate['tiao_id']]));
            }
            if($datacate['tiao_type']==2){
                redirect((string)url('index/show', ['id' => $datacate['tiao_id']]));
            }
        }

        if($action=='show'){
            if(config('app.debug',false)){
                $dataarticle=(new \app\model\ArticleModel())->find($id);
                if(!$dataarticle){
                    header("Location:/404.html");
                    die;
                }
                $datacate=(new \app\model\CateModel())->find($dataarticle['cateid']);
                $dataarticle['catename']=$datacate['catename'];
                $dataarticle['en_name']=$datacate['en_name'];
                $dataarticle['fid']=$datacate['fid'];
                $dataarticle['cateid']=$datacate['id'];
            }else{
                if(!$dataarticle=Cache::get('dataarticle'.$id)){
                    $dataarticle=(new \app\model\ArticleModel())->find($id);
                    if(!$dataarticle){
                        header("Location:/404.html");
                        die;
                    }
                    $datacate=(new \app\model\CateModel())->find($dataarticle['cateid']);
                    $dataarticle['catename']=$datacate['catename'];
                    $dataarticle['en_name']=$datacate['en_name'];
                    $dataarticle['fid']=$datacate['fid'];
                    $dataarticle['cateid']=$datacate['id'];
                    Cache::set('dataarticle'.$id,$dataarticle,3600);
                }
            }

            View::assign('data', $dataarticle);
        }

        $cateid=0;
        if($action=='show'){
            $cateid	=(new \app\model\ArticleModel())->where('id',$id)->value('cateid');
        }
        if($action=='cate'){
            $cateid = $id;
        }
        View::assign('cateid', $cateid);
    }
    /**
     * 操作成功跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  mixed     $data 返回的数据
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function success($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
    {
        if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
            $url = $_SERVER["HTTP_REFERER"];
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : app('route')->buildUrl($url);
        }

        $result = [
            'code' => 1,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];
        $type = $this->getResponseType();
        if ($type == 'html'){
            $response = view($this->app->config->get('app.dispatch_success_tmpl'), $result);
        } else if ($type == 'json') {
            $response = json($result);
        }
        throw new HttpResponseException($response);
    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param  mixed     $msg 提示信息
     * @param  string    $url 跳转的URL地址
     * @param  mixed     $data 返回的数据
     * @param  integer   $wait 跳转等待时间
     * @param  array     $header 发送的Header信息
     * @return void
     */
    protected function error($msg = '', string $url = null, $data = '', int $wait = 3, array $header = [])
    {
        if (is_null($url)) {
            $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);';
        } elseif ($url) {
            $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url);
        }

        $result = [
            'code' => 0,
            'msg'  => $msg,
            'data' => $data,
            'url'  => $url,
            'wait' => $wait,
        ];

        $type = $this->getResponseType();
        if ($type == 'html'){
            $response = view($this->app->config->get('app.dispatch_error_tmpl'), $result);
        } else if ($type == 'json') {
            $response = json($result);
        }
        throw new HttpResponseException($response);
    }



}